package com.dailyblue.java.mybatis.mybatis04;

import com.dailyblue.java.mybatis.mybatis04.bean.Emp;
import com.dailyblue.java.mybatis.mybatis04.mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class App {

    private static SqlSession session;
    private static EmpMapper mapper;

    static {
        try {
            InputStream in = Resources.getResourceAsStream("config.xml");
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
            session = ssf.openSession();
            mapper = session.getMapper(EmpMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // if
    private static void a() {
        String job = null;
        List<Emp> list = mapper.find1(job);
        list.forEach(System.out::println);
    }
    // where
    private static void b() {
        String job = null;
        Double sal = 3000.4;
        List<Emp> list = mapper.find2(job,sal);
        list.forEach(System.out::println);
    }
    // trim
    private static void c() {
        String job = "null";
        Double sal = 3000.4;
        List<Emp> list = mapper.find3(job,sal);
        list.forEach(System.out::println);
    }
    // choose、when和otherwise
    private static void d() {
        List<Emp> list = mapper.find4("null");
        list.forEach(System.out::println);
    }
    // foreach
    private static void e() {
        String[] jobs = {"aa","bb","cc"};
        List<Emp> list = mapper.find5(jobs);
        list.forEach(System.out::println);
    }
    // set
    private static void f1() {
        mapper.update1("",null,99988);
    }
    // set
    private static void f2() {
        mapper.update2("",null,99988);
    }
    // bind
    private static void g() {
        List<Emp> list = mapper.find6("A");
        list.forEach(System.out::println);
    }
    public static void main(String[] args) {
        g();
    }
}
